﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmParamEdit : BaseForm
    {
        public FrmParamEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        /// <summary>
        /// 页面初始化并绑定数据
        /// </summary>
        private async void Form_Load(object sender, EventArgs e)
        {
            await layoutControl.BindingModelAsync();
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private async void BtnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        /// <summary>
        /// 隐藏窗口
        /// </summary>
        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        private void LayoutControl_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () => bindingSource.DataSource = await SysParamService.Instance.GetOrCreateAsync(e.Args);
        }

        private void LayoutControl_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = () => SysParamService.Instance.SaveAsync(e.Args.Created, (SysParam)e.Model);
        }

        private async void LayoutControl_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysParamService.Instance.NewAsync();
            await layoutControl.BindingModelAsync();
        }
    }
}